package com.pie4cloud.pie.job.executor.config;

import com.pie4cloud.pie.job.executor.dto.StatisticalRequest;
import com.pie4cloud.pie.job.executor.enums.StatisticalType;
import com.pie4cloud.pie.job.executor.services.StatisticalBaseService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.EnumMap;
import java.util.Map;

@Component
public class StatisticalFactory implements InitializingBean, ApplicationContextAware {
	private static final Map<StatisticalType, StatisticalBaseService> STATISTICAL_SERVICE_MAP = new EnumMap<>(StatisticalType.class);
	private ApplicationContext appContext;

	@Override
	public void afterPropertiesSet() throws Exception {
		appContext.getBeansOfType(StatisticalBaseService.class)
				.values()
				.forEach(handler -> STATISTICAL_SERVICE_MAP.put(handler.getStatisticalType(), handler));
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
       this.appContext=applicationContext;
	}

	public StatisticalBaseService getHandler(StatisticalType statisticalType) {
		return STATISTICAL_SERVICE_MAP.get(statisticalType);
	}



}
